Passed
Pull Request — master (#436)
by
unknown
02:43
created

CreateNotificationCommandHandler   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 25
dl 0
loc 28
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A execute 0 19 2
1
import { Inject } from '@nestjs/common';
2
import { CommandHandler } from '@nestjs/cqrs';
3
import { Notification, NotificationType } from 'src/Domain/Notification/Notification.entity';
4
import { CreateNotificationCommand } from './CreateNotificationCommand';
5
import { INotificationRepository } from 'src/Domain/Notification/Repository/INotificationRepository';
6
import { IMattermostNotifier } from 'src/Application/IMattermostNotifier';
7
8
const { MATTERMOST_CHANNEL_LEAVES_ID } = process.env;
9
10
@CommandHandler(CreateNotificationCommand)
11
export class CreateNotificationCommandHandler {
12
  constructor(
13
    @Inject('INotificationRepository')
14
    private readonly notificationRepository: INotificationRepository,
15
    @Inject('IMattermostNotifier')
16
    private readonly mattermostNotifier: IMattermostNotifier,
17
    ) {}
18
19
  public async execute(command: CreateNotificationCommand): Promise<string> {
20
    const { message, type, leaveReaquest } = command;
21
22
    if (type === NotificationType.POST) {
23
      const { id } = await this.mattermostNotifier.createPost(MATTERMOST_CHANNEL_LEAVES_ID, message);
24
      const notification = await this.notificationRepository.save(
25
        new Notification(
26
          type,
27
          message,
28
          id,
29
          leaveReaquest
30
        )
31
      );
32
33
      return notification.getId();
34
    }
35
36
    throw new Error('Type not managed');
37
  }
38
}
39